<?php
class globalModel{
	public $db = null;
	public function __construct() {
		$this->db = &JFactory::getDBO();
	}

	public function getFirstImgURL($content) {
		$newStr = "";
		$signal = 'src=';
		$pos = strpos($content,$signal);
		$newStr = substr($content, $pos + 5);
		$pos = strpos($newStr,'"');
		$newStr = substr($newStr, 0, $pos);
		return $newStr;
	}
	public function getArticle($id) {
		$query = "select * from #__content where id = '{$id}' AND state=1";
		$this->db->setQuery( $query );
		$datas = $this->db->loadAssocList();		
		return $datas[0];
	}
	
	public function getAllArticle() {
		$query = "select * from #__content WHERE (state=1) AND (id <> 314) ORDER BY ordering ASC";
		$this->db->setQuery( $query );
		$datas = $this->db->loadAssocList();		
		return $datas;
	}
	
	public function getMWCItemId($catId) {
		$query = "select str_link from #__mwc_itemid where cat_id = '{$catId}'";
		$this->db->setQuery( $query );
		$datas = $this->db->loadAssocList();
		return $datas[0];
	}
	
	public function getArticleByCatId($catId, $contentId = null) {
		$query = "select * from #__content where catid = '{$catId}' AND (state=1)";
		if ( $contentId != null )
			$query .= " AND id <> ".$contentId;
		$query .= " ORDER BY ordering ASC LIMIT 0 , 2";
		$this->db->setQuery( $query );
		$datas = $this->db->loadAssocList();
		return $datas;
	}
	
	public function deleteArticle($id) {
		$query = "delete from #__content where id = '{$id}'";
		return $this->db->execute($query);
	}
	
	public function getIntroduce()
	{
		$ar = $this->getArticle(49);
		$ar['introtext'] = $this->getShortText($ar['introtext'], 1000);
		return $ar;
	}
	
	public function getProductCategories()
	{
		$query = "select * from #__mwc_categories order by ordering ASC";
		$this->db->setQuery( $query );
		$datas = $this->db->loadAssocList();
		return $datas;
	}	
	
	public function getVMCategories()
	{
		$query = "select category_id, category_name from #__vm_category order by category_id ASC";
		$this->db->setQuery( $query );
		$datas = $this->db->loadAssocList();
		return $datas;
	}	
	
	public function getCategoryName($id) {
		$query = "select * from #__vm_category where category_id = '{$id}'";
		$this->db->setQuery( $query );
		$datas = $this->db->loadAssocList();
		return $datas[0];
	}
	
	public function getProducts()
	{
		$query = "SELECT * FROM #__vm_product ORDER BY product_id DESC LIMIT 0 , 2";
		$this->db->setQuery( $query );
		$datas = $this->db->loadAssocList();
		return $datas;
	}
	
	public function getMWCImages($id = null)
	{
		$query = "SELECT * FROM #__mwc_image ";
		if ( $id > 0 )
			$query .= " WHERE id = {$id} AND published=1";
//		else
//			$query .= " ORDER BY ordering LIMIT 0,4";
		$this->db->setQuery( $query );
		$datas = $this->db->loadAssocList();
		return $datas;
	}
	
	public function getOptionsDetail($id)
	{
		$query = "select * from #__mwc_options_detail where option_id = '{$id}' order by ordering ASC";
		$this->db->setQuery( $query );
		$datas = $this->db->loadAssocList();
		return $datas;
	}		
	
	public function getOptionsDetailFromId($id)
	{
		$query = "select * from #__mwc_options_detail where id = '{$id}' order by ordering ASC";
		$this->db->setQuery( $query );
		$datas = $this->db->loadAssocList();
		return $datas[0];
	}		
	
	public function getShortText($text, $startNum = 40) {
//		$text = strip_tags($text);
		$text = strip_tags($text,'<strong><br>');
		if(strlen($text) <= $startNum) {
			return $text;
		}
		$char = "p";
		while($char != " ") {
			$char = substr($text,$startNum, 1);
			$startNum++;
		}
		return substr($text,0, $startNum);
	}
	
	public function isHomePage()
	{
		$option = JRequest::getCmd('option');
		$view = JRequest::getCmd('view');

		if ($option == 'com_content' && $view == 'frontpage') { 
			return true;
		} else {
			return false;
		}
	}
		
	public function calYearRegister()
	{
		$query = "SELECT *, (YEAR(CURDATE())-YEAR(registerDate)) - (RIGHT(CURDATE(),5)<RIGHT(registerDate,5)) AS year, (YEAR(DATE_SUB(CURDATE(), INTERVAL 14 DAY))-YEAR(registerDate)) - (RIGHT(DATE_SUB(CURDATE(), INTERVAL 14 DAY),5)<RIGHT(registerDate,5)) AS beforeyear, (CURDATE()-registerDate) as calDay FROM jos_users WHERE block=0";		
		$this->db->setQuery( $query );
		$datas = $this->db->loadAssocList();
		return $datas;
	}
	
	public function getEmailUserByID($uid)
	{
		$query = "SELECT * FROM jos_users WHERE id=".$uid;		
		$this->db->setQuery( $query );
		$datas = $this->db->loadAssocList();
		return $datas[0];
	}
}